﻿using HlELock.Domain;
using HlELock.Domain.Entities;
using Microsoft.EntityFrameworkCore.Internal;
using Prism.Commands;
using System.Collections.ObjectModel;
using System.Linq;
using System.Windows;
using Mapster;
using HlELock.Core;


namespace HlELock.Modules.Hotel.Main.ViewModels
{
    public class RoomTypePageViewModel : DialogViewModelBase
    {
        public RoomTypePageViewModel()
        {
            CurrentRoomType = new RoomType();
            LoadData();
        }
        private ObservableCollection<RoomType> _roomTypes;
        public ObservableCollection<RoomType> RoomTypes
        {
            get { return _roomTypes; }
            set { SetProperty(ref _roomTypes, value); }
        }

        private RoomType _currentRoomType;
        public RoomType CurrentRoomType
        {
            get { return _currentRoomType; }
            set { SetProperty(ref _currentRoomType, value); }
        }

        private DelegateCommand _addCommand;
        public DelegateCommand AddCommand =>
            _addCommand ?? (_addCommand = new DelegateCommand(ExecuteAddCommand));

        /// <summary>
        /// 增加
        /// </summary>
        void ExecuteAddCommand()
        {
            using (var context = new HlELockDbContext())
            {
                if (CurrentRoomType == null || string.IsNullOrWhiteSpace(CurrentRoomType.Name))
                {
                    MessageBox.Show(AppHelper.GetResourceString("QingTianXie", "KeFangLeiXing"));
                    return;
                }
                if (CurrentRoomType.Id != default)
                {
                    CurrentRoomType.Id = default;
                    CurrentRoomType = CurrentRoomType.Adapt<RoomType>();
                }

                CurrentRoomType.Name = CurrentRoomType.Name.Trim();
                if (context.RoomTypes.Any(a => a.Name == CurrentRoomType.Name))
                {
                    MessageBox.Show($"{AppHelper.GetResourceString("KeFangLeiXing")}【{CurrentRoomType.Name}】{AppHelper.GetResourceString("YiJingCunZai")}");
                    return;
                }
                context.Add(CurrentRoomType);
                CurrentRoomType.Index = RoomTypes.Count + 1;
                context.SaveChanges();
                LoadData();

            }

        }

        /// <summary>
        /// 刷新房间类型序号
        /// </summary>
        void RefreshRoomTypesIndex()
        {
            for (var i = 0; i < RoomTypes.Count; i++)
            {
                RoomTypes[i].Index = i + 1;
            }

        }
        private DelegateCommand _deleteCommand;
        public DelegateCommand DeleteCommand =>
            _deleteCommand ?? (_deleteCommand = new DelegateCommand(ExecuteDeleteCommand));

        /// <summary>
        /// 删除
        /// </summary>
        void ExecuteDeleteCommand()
        {
            if (CurrentRoomType != null && CurrentRoomType.Id != default)
            {
                using (var context = new HlELockDbContext())
                {
                    var entity = context.RoomTypes.Find(CurrentRoomType.Id);
                    context.Remove(entity);
                    RefreshRoomTypesIndex();
                    context.SaveChanges();
                }
                MessageBox.Show(AppHelper.GetResourceString("ShanChuChengGong"));
                LoadData();
            }
        }

        private DelegateCommand _editCommand;
        public DelegateCommand EditCommand =>
            _editCommand ?? (_editCommand = new DelegateCommand(ExecuteEditCommand));

        /// <summary>
        /// 修改
        /// </summary>
        void ExecuteEditCommand()
        {
            if (CurrentRoomType.Id == default)
            {
                MessageBox.Show(AppHelper.GetResourceString("QingXuanZeShuJu"));
            }
            using (var context = new HlELockDbContext())
            {
                var entity = context.RoomTypes.Find(CurrentRoomType.Id);
                CurrentRoomType.Adapt(entity);
                context.SaveChanges();
            }
            MessageBox.Show(AppHelper.GetResourceString("XiuGaiChengGong"));
        }

        void LoadData()
        {
            using (var context = new HlELockDbContext())
            {
                var datas = context.RoomTypes.OrderBy(o => o.Index).ToList();
                RoomTypes = datas.ToObservableCollection();
                CurrentRoomType = new RoomType();
            }
        }
    }
}
